/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Group 3 Aprotrain Aptech  Group3 Aprotrain Aptech
 */
public class DBHandler {
    private static String username = "sa";
    private static String password = "1234567";
    private static String url = "jdbc:sqlserver://localhost:1433;databasename = Mail_Server;";

    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        Connection conn = null;
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        conn = DriverManager.getConnection(DBHandler.url, DBHandler.username, DBHandler.password);
        return conn;
   }

    public static void closeDBObj(Object dbobj) throws SQLException{
        if(dbobj !=null){
            if(dbobj instanceof Connection){
                ((Connection)dbobj).close();
            }else if(dbobj instanceof ResultSet){
                ((ResultSet)dbobj).close();
            }else if(dbobj instanceof Statement){
                ((Statement)dbobj).close();
            }
        }
    }

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Connection connection = getConnection();

        if(connection == null){
            System.out.println("Not OK");
        }else {
            System.out.println("OK");
        }

        closeDBObj(connection);
    }
}
